package projetoeCommerce.Dados;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import projetoeCommerce.Entidades.Cliente;


public class DadosCliente {

	

		public void Inserir(Cliente c) throws SQLException
		{
			Connection conexao = Conexao.GetConexao();
			try {

				Statement stm = conexao.createStatement();

				StringBuilder sb = new StringBuilder("Insert into Cliente " +
						"(Nome, Email, Telefone, Cidade, UsuarioLoginSistema, SenhaSistema)");
				
				sb.append("values (");
				sb.append("'" + c.Nome + "', ");
				sb.append("'" + c.get_email() + "', ");
				sb.append("'" + c.Telefone + "', ");
				sb.append("'" + c.Cidade + "', ");
				sb.append("'" + c.UsuarioLoginSistema + "', ");
				sb.append("'" + c.SenhaSistema + "' ");
				sb.append(") ");
				
				
				
				System.out.println(sb.toString());
				stm.execute(sb.toString());
			}
			finally
			{
				conexao.close();
			}
		}

		public void Alterar(Cliente c) throws SQLException
		{
			Connection conexao = Conexao.GetConexao();
			try {

				Statement stm = conexao.createStatement();

				StringBuilder sb = new StringBuilder("Update Cliente SET ");
				
				sb.append("Nome = '" + c.Nome + "', ");
				sb.append("Email = '" + c.get_email() + "', ");
				sb.append("Telefone = '" + c.Telefone + "', ");
				sb.append("Cidade = '" + c.Cidade + "', ");
				sb.append("UsuarioLoginSistema = '" + c.UsuarioLoginSistema + "', ");
				sb.append("SenhaSistema = '" + c.SenhaSistema + "' ");
				
				sb.append("WHERE CodCliente = " + c.get_codigo());
				
				System.out.println(sb.toString());
				stm.execute(sb.toString());
			}
			finally
			{
				conexao.close();
			}
		}

		public void Remover(Cliente c) throws SQLException
		{
			Remover(c.get_codigo());
		}
		
		public void Remover(int codigo) throws SQLException
		{
			Connection conexao = Conexao.GetConexao();
			try {

				Statement stm = conexao.createStatement();

				StringBuilder sb = new StringBuilder("Delete from Cliente ");
				sb.append("WHERE CodCliente = " + codigo);
				
				System.out.println(sb.toString());
				stm.execute(sb.toString());
			}
			finally
			{
				conexao.close();
			}
		}

		public Cliente GetPorCodigo(int codigo) throws SQLException
		{
			Cliente c = null;

			Connection conexao = Conexao.GetConexao();
			try {

				Statement stm = conexao.createStatement();

				StringBuilder sb = new StringBuilder("Select * from Cliente ");
				sb.append("WHERE CodCliente = " + codigo);
			
				System.out.println(sb.toString());
				ResultSet rs = stm.executeQuery(sb.toString());

				if (rs.next()) {
					 c = ControiProdutoComORS(rs); 
				}

				rs.close();
				stm.close();

			}
			finally
			{
				conexao.close();
			}
			return c;
		}

		public HashMap<String, Cliente> getTodos() throws SQLException{

			HashMap<String, Cliente> clientes = new HashMap<String, Cliente>();

			Connection conexao = Conexao.GetConexao();
			try {

				Statement stm = conexao.createStatement();

				StringBuilder sb = new StringBuilder("Select * from Cliente ");
			
				System.out.println(sb.toString());
				ResultSet rs = stm.executeQuery(sb.toString());

				while (rs.next()) {
					Cliente c = ControiProdutoComORS(rs);
					clientes.put(c.get_codigo() + "", c);
				}

				rs.close();
				stm.close();

			}
			finally
			{
				conexao.close();
			}

			return clientes;	
		}

		private Cliente ControiProdutoComORS(ResultSet rs) throws SQLException {
			  Cliente c = new Cliente();
			  
			  c.set_codigo(rs.getInt("CodCliente")); 
			  c.Nome = rs.getString("Nome");
			  c.set_email(rs.getString("Email"));
			  c.Telefone = rs.getString("Telefone");
			  c.Cidade = rs.getString("Cidade"); 
			  c.UsuarioLoginSistema = rs.getString("UsuarioLoginSistema");
			  c.SenhaSistema = rs.getString("SenhaSistema");
			  
			  return c;
		}

	
}
